const {
  controller
} = require('../../controllers/controllerIndex.js');

const Controllers = require('../../controllers/Controller.js').controller;
const BASE_URL = require('../../../utils/kzj.request.js').url;

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '新增商品',
    isDown: false,
    goodsImg: null,
    name: '',
    costPrice: '',
    salesPrice: '',
    scribingPrice: '',
    memberPrice: '',
    vipPrice: '',
    array: [{
      name: "",
      id: 1
    }],
    setCommission: 0,
    isAddGoods: false,
    swiperImgList: [],
    addSwiperImgList: [],
    swiperImgIds: [],
    detailsImgList: [],
    addDetailsImgList: [],
    detailsImgIds: [],
    vip: false,
    memberPriceState: false,
    vipPriceState: false,
    integralState: false,
    stockTypeState: false,
    heat: true,
    labelId: '',
    type: 1,
    rewardPrice: '',
    rewardProportion: '',
    stock: 2000,
    stockType: '',
    warnCount: 1,
    goodsDetailsdata: '',
    goodsId: '',
    labelindex: 0,
    deleteRotationImgIds: [],
    deleteDetailImgIds: [],
    integral: '',
    isedit: true,
    rotationImgs: [],
    detailImgs: [],
    buttons: [{
      text: '取消'
    }, {
      text: '确定'
    }],
    dialogShow: false,
    vipState: 0,
    memberState: 0,
    isOpenBean: true,
    openBean: 1,
    isOpenActivity: false,
    openActivity: 0,

    openShareholder: false,
    shareholderState: 0,
    shareholderPrice: '',
    shareholderPriceState: false,
    getAgentPrice: "",
    openAgent: false,
    openGoShop: false,
    openGoHome: false,
    videoSrc: '',
    shamSellCount: 0
  },
  //输入框赋值
  bindinput(e) {
    let type = e.currentTarget.dataset.type;
    let value = e.detail.value
    if (type == "name") {
      this.setData({
        name: value
      })
    } else if (type == "costPrice") {
      this.setData({
        costPrice: value
      })
    } else if (type == "salesPrice") {
      this.setData({
        salesPrice: value
      })
    } else if (type == "memberPrice") {
      if (this.data.vipPriceState == false) {
        this.setData({
          vipPrice: value,
        })
      }
      // if (this.data.shareholderPriceState == false) {
      //   this.setData({
      //     shareholderPrice: value,
      //   })
      // }
      this.setData({
        memberPrice: value
      })
    } else if (type == "vipPrice") {
      this.setData({
        vipPrice: value
      })
    } else if (type == "shareholderPrice") {
      this.setData({
        shareholderPrice: value
      })
    } else if (type == 'desc') {
      this.setData({
        desc: value
      })
    } else if (type == 'rewardPrice') {
      this.setData({
        rewardPrice: value
      })
    } else if (type == 'rewardProportion') {
      this.setData({
        rewardProportion: value
      })
    } else if (type == 'stock') {
      this.setData({
        stock: value
      })
    } else if (type == 'warnCount') {
      this.setData({
        warnCount: value
      })
    } else if (type == 'shamSellCount') {
      value = Number(value)
      value = value < 0 ? 0 : value > 1000 ? 1000 : value
      this.setData({
        shamSellCount: value
      })
    } else if (type == 'integral') {
      this.setData({
        integral: value
      })
    }
  },
  onLoad: function (options) {
    let title
    this.selectLabel()
    if (options.type) {
      this.setData({
        type: options.type
      })

      if (options.type == 1) {
        title = "新增商品"
      } else {
        title = "新增服务"
      }

    }
    if (options.isAddGoods) {
      this.setData({
        isAddGoods: options.isAddGoods
      })
    } else {
      if (options.id) {
        this.setData({
          goodsId: options.id
        })
        this.goodsDetails()
      }

      if (options.type == 1) {
        title = "编辑商品"
      } else {
        title = "编辑服务"
      }

    }
    if (options.isedit) {
      this.setData({
        isedit: options.isedit == '0' ? false : true
      })
    }
    this.setData({
      title
    })
  },
  selectLabel() {
    Controllers.selectLabel({
      type: 1,
      shopId: wx.getStorageSync('shopId')
    }).then(res => {
      this.setData({
        array: res.data,
        labelId: res.data[0].id,
      })
    });
  },
  switchDown(e) {
    this.setData({
      isDown: e.detail.value
    })
  },

  switchvip(e) {
    this.setData({
      vip: e.detail.value,
    })
    if (this.data.vip == true) {
      this.setData({
        heat: false,
        openShareholder: false,
        openAgent: false,

      })
    }
  },

  // 打开股东专享按钮
  switchshareholder(e) {
    this.setData({
      openShareholder: e.detail.value,
    })
    if (this.data.openShareholder == true) {
      this.setData({
        heat: false,
        vip: false,
        openAgent: false,
      })
    }
  },

  // 打开代理商专享按钮
  switchAgent(e) {
    console.log(e.detail.value);
    this.setData({
      openAgent: e.detail.value,
    })
    if (this.data.openAgent == true) {
      this.setData({
        heat: false,
        vip: false,
        openShareholder: false,
      })
    }
  },

  switchheat(e) {
    this.setData({
      heat: e.detail.value,
    })
    if (this.data.heat == true) {
      this.setData({
        vip: false,
        openShareholder: false,
        openAgent: false,
      })
    }
  },


  switchStockType(e) {
    this.setData({
      stockTypeState: e.detail.value,
    })
  },

  switchGoShopType(e) {
    this.setData({
      openGoShop: e.detail.value,
    })
  },
  switchGoHomeType(e) {
    this.setData({
      openGoHome: e.detail.value,
    })
  },
  switchmemberPriceState(e) {
    this.setData({
      memberPriceState: e.detail.value
    })
    if (e.detail.value == true) {
      this.setData({
        memberState: 1
      })
    } else {
      this.setData({
        memberState: 0
      })
    }
  },

  switchvipPriceState(e) {
    this.setData({
      vipPriceState: e.detail.value
    })
    if (e.detail.value == true) {
      this.setData({
        vipState: 1
      })
    } else {
      this.setData({
        vipState: 0
      })
    }
    // if (this.data.vipPriceState == false) {
    //   this.setData({
    //     vipPrice: this.data.memberPrice
    //   })
    // }
  },

  // switchShareholderPriceState(e) {
  //   this.setData({
  //     shareholderPriceState: e.detail.value
  //   })
  //   if(e.detail.value==true){
  //     this.setData({
  //       shareholderState:1
  //     })
  //   }else{
  //     this.setData({
  //       shareholderState:0
  //     })
  //   }
  //   if (this.data.shareholderPriceState == false) {
  //     this.setData({
  //       shareholderPrice: this.data.memberPrice
  //     })
  //   }
  // },

  switchintegralState(e) {
    this.setData({
      integralState: e.detail.value
    })
  },

  switchOpenBean(e) {
    this.setData({
      isOpenBean: e.detail.value,
    })
    if (this.data.isOpenBean == false) {
      this.setData({
        openBean: 0
      })
    } else {
      this.setData({
        openBean: 1
      })
    }
  },

  switchOpenActivity(e) {
    this.setData({
      isOpenActivity: e.detail.value,
    })
    if (this.data.isOpenActivity == false) {
      this.setData({
        openActivity: 0
      })
    } else {
      this.setData({
        openActivity: 1
      })
    }
  },

  //跳转图库
  goodsImgToGallery() {
    let that = this
    wx.showModal({
      title: "提示",
      content: "请选择上传图片方式",
      confirmText: "图库",
      cancelText: "相册",
      success(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/gallery/gallery?goodsImg=1&upNum=1',
          })
        } else if (res.cancel) {
          that.choose()
        }
      }
    })
  },

  choose() {
    if (!this.data.isedit) {
      return
    }
    let that = this;
    let swiperImgList = this.data.swiperImgList
    let addSwiperImgList = this.data.addSwiperImgList
    wx.chooseImage({
      count: 1,
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://www.xlshhr.com/beauty-industry/api/img/uploadFile',
          filePath: tempFilePaths[0],
          name: 'image',
          formData: {
            'Content-Type': 'application/form-data',
            'sessionId': wx.getStorageSync('token')
          },
          success(res) {
            console.log(res);
            let src = JSON.parse(res.data)
            swiperImgList.push(src.data)
            addSwiperImgList.push(src.data)
            that.setData({
              goodsImg: src.data,
              swiperImgList: swiperImgList,
              addSwiperImgList: addSwiperImgList
            })
          }
        })
      }
    })
  },

  //轮播图选择图库
  bannerToGallery() {
    let that = this
    wx.showModal({
      title: "提示",
      content: "请选择上传图片方式",
      confirmText: "图库",
      cancelText: "相册",
      success(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/gallery/gallery?bannerImg=1&upNum=5',
          })
        } else if (res.cancel) {
          that.choosebanner()
        }
      }
    })
  },

  choosebanner() {
    if (!this.data.isedit) {
      return
    }
    let that = this;
    if (this.data.swiperImgList >= 5) {
      wx.showToast({
        title: '轮播图最多上传5张！',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let length = this.data.swiperImgList.length
    wx.chooseImage({
      count: 5 - length,
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        const tempFiles = res.tempFiles;
        console.log("tempFiles", tempFiles)
        that.uploadBanner(tempFilePaths, tempFilePaths.length, 0)
      }
    })
  },
  uploadBanner(tempFilePaths, leng, index) {
    let that = this
    wx.uploadFile({
      url: 'https://www.xlshhr.com/beauty-industry/api/img/uploadFile',
      filePath: tempFilePaths[index],
      name: 'image',
      formData: {
        'Content-Type': 'application/form-data',
        'sessionId': wx.getStorageSync('token')
      },
      success(res) {
        console.log(res);
        let src = JSON.parse(res.data)
        console.log("src", src)
        let swiperImgList = that.data.swiperImgList
        let addSwiperImgList = that.data.addSwiperImgList
        swiperImgList.push(src.data)
        addSwiperImgList.push(src.data)
        that.setData({
          swiperImgList,
          addSwiperImgList
        })
        if (leng == index) {
          return
        } else {
          index++
          that.uploadBanner(tempFilePaths, leng, index)
        }
      }
    })
  },

  //详情图选择图库
  detailToGallery() {
    let that = this
    wx.showModal({
      title: "提示",
      content: "请选择上传图片方式",
      confirmText: "图库",
      cancelText: "相册",
      success(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/gallery/gallery?detailImg=1&&upNum=9',
          })
        } else if (res.cancel) {
          that.choosedetail()
        }
      }
    })
  },

  choosedetail() {
    if (!this.data.isedit) {
      return
    }
    let that = this;
    let length = this.data.detailsImgList.length
    wx.chooseImage({
      count: 9 - length,
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        const tempFiles = res.tempFiles;
        // for (let i = 0; i < tempFilePaths.length; i++) {
        //   if (tempFilePaths[i].size > 1024 * 1024) {
        //     wx.showToast({
        //       title: '单张图片大不能超过1MB',
        //       icon: 'none',
        //       duration: 2000
        //     })
        //     return
        //   }
        // }
        that.uploadDdetail(tempFilePaths, tempFilePaths.length, 0)
      }
    })
  },
  uploadDdetail(tempFilePaths, leng, index) {
    let that = this
    wx.uploadFile({
      url: 'https://www.xlshhr.com/beauty-industry/api/img/uploadFile',
      filePath: tempFilePaths[index],
      name: 'image',
      formData: {
        'Content-Type': 'application/form-data',
        'sessionId': wx.getStorageSync('token')
      },
      success(res) {
        console.log(res);
        let src = JSON.parse(res.data)
        console.log("src", src)
        let detailsImgList = that.data.detailsImgList
        let addDetailsImgList = that.data.addDetailsImgList
        detailsImgList.push(src.data)
        addDetailsImgList.push(src.data)
        that.setData({
          detailsImgList,
          addDetailsImgList
        })
        if (leng == index) {
          return
        } else {
          index++
          that.uploadDdetail(tempFilePaths, leng, index)
        }
      }
    })
  },
  bindPickerChange(e) {
    console.log(e)
    this.setData({
      labelId: this.data.array[e.detail.value].id,
      labelindex: e.detail.value
    })
  },
  setCommiss(e) {
    if (!this.data.isedit) {
      return
    }
    this.setData({
      setCommission: e.currentTarget.dataset.type
    })
  },
  goodsDetails() {
    Controllers.goodsDetails({
      goodsId: this.data.goodsId
    }).then(res => {
      if (res.data.labelName.indexOf("/")) {
        res.data.labelName = res.data.labelName.split('/')
      } else {
        res.data.labelName = [res.data.labelName]
      }
      this.data.array.forEach((item, index) => {
        console.log("item", item)
        if (item.name == res.data.labelName) {
          this.setData({
            labelindex: index,
            labelId: this.data.array[index].id,
          })
        }
      });
      let swiperImgList = this.data.swiperImgList
      let swiperImgIds = this.data.swiperImgIds
      res.data.rotationImgs.forEach(item => {
        swiperImgList.push(item.image)
        swiperImgIds.push(item.id)
      })
      console.log("swiperImgList", swiperImgList)
      let detailsImgList = this.data.detailsImgList
      let detailsImgIds = this.data.detailsImgIds
      res.data.detailImgs.forEach(img => {
        detailsImgList.push(img.image)
        detailsImgIds.push(img.id)
      })
      console.log("detailsImgList", detailsImgList)
      this.setData({
        goodsDetailsdata: res.data,
        isDown: res.data.state == 1 ? true : false,
        goodsImg: res.data.image,
        name: res.data.name,
        costPrice: res.data.oldPrice,
        salesPrice: res.data.price,
        vipPrice: res.data.vipPrice,
        shareholderPrice: res.data.shareholderPrice,
        agentPrice: res.data.agentPrice,
        memberPrice: res.data.memberPrice,
        setCommission: res.data.rewardState,
        swiperImgList,
        rotationImgs: res.data.rotationImgs,
        detailImgs: res.data.detailImgs,
        swiperImgIds,
        detailsImgList,
        detailsImgIds,
        vip: res.data.vip == 1 ? true : false,
        openShareholder: res.data.openShareholder == 1 ? true : false,
        openAgent: res.data.openAgent == 1 ? true : false,
        integralState: res.data.integralState == 1 ? true : false,
        integral: res.data.integral ? res.data.integral : '',
        heat: res.data.heat == 1 ? true : false,
        rewardPrice: res.data.rewardPrice,
        rewardProportion: res.data.rewardProportion,
        stock: res.data.stock,
        warnCount: res.data.warnCount,
        shamSellCount: res.data.shamSellCount,
        desc: res.data.content,
        stockType: res.data.stockType,
        vipState: res.data.vipState,
        memberState: res.data.memberState,
        openBean: res.data.openBean,
        openActivity: res.data.openActivity,
        openGoHome: res.data.openGoHome,
        videoSrc: res.data.video,
      })
      if (this.data.vipState == 1) {
        this.setData({
          vipPriceState: true
        })
      } else {
        this.setData({
          vipPriceState: false
        })
      }

      if (this.data.openBean == 1) {
        this.setData({
          isOpenBean: true
        })
      } else {
        this.setData({
          isOpenBean: false
        })
      }
      if (this.data.openActivity == 1) {
        this.setData({
          isOpenActivity: true
        })
      } else {
        this.setData({
          isOpenActivity: false
        })
      }
      if (this.data.memberState == 1) {
        this.setData({
          memberPriceState: true
        })
      } else {
        this.setData({
          memberPriceState: false
        })
      }
      if (res.data.stockType == 0) {
        this.setData({
          stockTypeState: false
        })
      } else {
        this.setData({
          stockTypeState: true
        })
      }
      console.log("swiperImgIds", swiperImgIds)
    })
  },
  delectswiper(e) {
    if (!this.data.isedit) {
      return
    }
    let index = e.currentTarget.dataset.index;
    let itemsrc = e.currentTarget.dataset.src;
    let swiperImgList = this.data.swiperImgList
    // if (this.data.swiperImgIds.length > 0) {
    //   for(let item of this.data.swiperImgIds){
    //     if(itemsrc==item.image){
    //       let deleteRotationImgIds = this.data.deleteRotationImgIds
    //       deleteRotationImgIds.push(item.id)
    //       this.setData({
    //         deleteRotationImgIds
    //       })
    //     }
    //   }

    // }
    swiperImgList.splice(index, 1)
    this.setData({
      swiperImgList
    })
  },
  delectdetails(e) {
    if (!this.data.isedit) {
      return
    }
    let index = e.currentTarget.dataset['index'];
    let detailsImgList = this.data.detailsImgList;
    // let deleteDetailImgIds = this.data.deleteDetailImgIds
    // let itemsrc = e.currentTarget.dataset.src;
    // if (this.data.swiperImgIds.length > 0) {
    //   for(let item of this.data.detailsImgIds){
    //     if(itemsrc==item.image){
    //       let deleteDetailImgIds = this.data.deleteDetailImgIds
    //       deleteDetailImgIds.push(item.id)
    //       this.setData({
    //         deleteDetailImgIds
    //       })
    //     }
    //   }

    // }

    detailsImgList.splice(index, 1)
    this.setData({
      detailsImgList,
      // deleteDetailImgIds
    })
  },
  // 视频选择
  chooseMedia() {
    const isLimitVideo = wx.getStorageSync('isLimitVideo')
    if (!isLimitVideo) {
      wx.showToast({
        title: '上传视频数已达上限，如需使用，请续费',
        icon: 'none',
        duration: 2600
      })
      return
    }
    let that = this;
    wx.chooseMedia({
      count: 1,
      mediaType: ['video'],
      success(res) {
        const tempFilePath = res.tempFiles[0].tempFilePath;
        const size = res.tempFiles[0].size;
        console.log(size);
        const duration = res.tempFiles[0].duration;
        if (size > 1024 * 1000 * 50) {
          wx.showToast({
            title: '上传视频不可大于20M',
            icon: 'none'
          })
          return
        } else if (duration > 20) {
          wx.showToast({
            title: '上传视频不可大于20秒',
            icon: 'none'
          })
          return
        }
        that.uploadVideo(tempFilePath)
      }
    })
  },
  // 压缩并上传视频
  uploadVideo(path) {
    // 压缩
    let that = this;
    wx.compressVideo({
      src: path,
      quality: 'high',
      success(res) {
        console.log(res);
        wx.showToast({
          title: '视频上传时间较长,请勿操作，耐心等待',
          icon: 'none',
          duration: 2000,
        })
        let timer = setTimeout(() => {
          wx.showLoading();
        }, 2000)

        // 上传
        wx.uploadFile({
          url: BASE_URL + '/api/img/uploadFile',
          // filePath: res.tempFilePath,
          filePath: path,
          name: 'image',
          formData: {
            'Content-Type': 'application/form-data',
            'sessionId': wx.getStorageSync('token')
          },
          success(res) {
            wx.hideLoading();
            clearTimeout(timer)

            let src = JSON.parse(res.data)
            that.setData({
              videoSrc: src.data
            })
          }
        })

      }
    })

  },
  delectVideo() {
    this.setData({
      videoSrc: '',
    })
  },
  save() {
    // if (!this.data.openGoShop&&!this.data.openGoHome) {
    //   wx.showModal({
    //     title: '提示',
    //     content:"至少选择一种配送方式"
    //   })
    //   return
    // }

    if (this.data.stockTypeState == false) {
      this.setData({
        stockType: 0
      })
    } else {
      this.setData({
        stockType: 1
      })
    }
    // if (this.data.vipPriceState == false) {
    //   this.setData({
    //     vipPrice: this.data.memberPrice
    //   })
    // }
    if (!this.data.labelId) {
      wx.showToast({
        title: '请选择分类',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!this.data.salesPrice) {
      wx.showToast({
        title: '请输入销售价',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!this.data.name) {
      wx.showToast({
        title: '请输入商品名称',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!this.data.goodsImg) {
      wx.showToast({
        title: '请上传商品缩略图',
        icon: 'none',
        duration: 2000
      })
      return
    }
    // if (!this.data.videoSrc) {
    //   wx.showToast({
    //     title: '请上传视频',
    //     icon: 'none',
    //     duration: 2000
    //   })
    //   return
    // }
    if (this.data.setCommission == 1 && !this.data.rewardProportion) {
      wx.showToast({
        title: '请设置提成百分比',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.setCommission == 2 && !this.data.rewardPrice) {
      wx.showToast({
        title: '请设置提成固定金额',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.type == 1 && !this.data.stock && this.data.stockType == 0) {
      wx.showToast({
        title: '请设置商品库存',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.type == 1 && !this.data.warnCount) {
      wx.showToast({
        title: '请设置预警库存',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.integralState == 1 && !this.data.integral) {
      wx.showToast({
        title: '请填入兑换积分',
        icon: 'none',
        duration: 2000
      })
      return
    }



    let data = {}
    data.shopId = wx.getStorageSync('shopId');
    data.labelId = this.data.labelId;
    data.state = this.data.isDown ? 1 : 0;
    data.rewardState = this.data.setCommission;
    data.integralState = this.data.integralState ? 1 : 0;
    data.vip = this.data.vip ? 1 : 0;
    data.openShareholder = this.data.openShareholder ? 1 : 0;
    data.openAgent = this.data.openAgent ? 1 : 0;
    data.type = this.data.type;
    data.heat = this.data.heat ? 1 : 0;
    data.oldPrice = this.data.costPrice || '';
    data.price = this.data.salesPrice || '';
    data.stockType = this.data.stockType;
    data.name = this.data.name;
    data.content = this.data.desc || '';
    data.image = this.data.goodsImg;
    data.video = this.data.videoSrc || '';
    data.integral = this.data.integral;
    data.rewardProportion = this.data.rewardProportion ? this.data.rewardProportion : '';
    data.stock = this.data.stock ? this.data.stock : '';
    data.warnCount = this.data.warnCount ? this.data.warnCount : '';
    data.shamSellCount = this.data.shamSellCount;
    data.memberPrice = this.data.memberPrice || ''
    data.vipPrice = this.data.vipPrice || '';
    data.shareholderPrice = this.data.shareholderPrice || '';
    data.agentPrice = this.data.agentPrice || '';
    data.rewardPrice = this.data.rewardPrice ? this.data.rewardPrice : '';
    data.rotationImgs = this.data.swiperImgList.join(',')
    data.vipState = this.data.vipState
    data.shareholderState = this.data.shareholderState
    data.memberState = this.data.memberState
    data.openBean = this.data.openBean
    data.openActivity = this.data.openActivity
    data.openGoHome = this.data.openGoHome ? 1 : 0
    // data.openGoShop=this.data.openGoShop ? 1 : 0
    let detailsImgList = this.data.detailsImgList
    if (this.data.deleteDetailImgIds.length > 0) {
      for (let i = 0; i < this.data.deleteDetailImgIds.length; i++) {
        for (let j = 0; j < this.data.detailsImgList.length; j++) {
          if (this.data.deleteDetailImgIds[i].image == this.data.detailsImgList[j]) {
            detailsImgList.splice(j, 1)
          }
        }
      }
    }
    data.detailImgs = detailsImgList.join(',')
    //判断权重是否重复
    if (this.data.isAddGoods) {
      console.log(data)
      Controllers.saveGoods(data).then(res => {
        wx.showToast({
          title: '保存成功！',
          icon: 'success',
          duration: 2000
        })
        setTimeout(res => {
          wx.navigateBack({ //返回
            delta: 1
          })
        })
      });
    } else {

      data.deleteRotationImgIds = this.data.swiperImgIds.join(',')
      data.deleteDetailImgIds = this.data.detailsImgIds.join(',')
      data.id = this.data.goodsId
      data.rotationImgs = this.data.swiperImgList.join(',')
      data.detailImgs = this.data.detailsImgList.join(',')
      console.log(data)

      Controllers.updateGoods(data).then(res => {
        wx.showToast({
          title: '保存成功！',
          icon: 'success',
          duration: 2000
        })
        setTimeout(res => {
          wx.navigateBack({ //返回
            delta: 1
          })
        })
      });
    }
  },
  delete() {
    let data = {
      shopId: wx.getStorageSync('shopId'),
      goodsId: this.data.goodsId
    }
    Controllers.delGoods(data).then(res => {
      wx.showToast({
        title: '删除成功！',
        icon: 'success',
        duration: 2000
      })
      setTimeout(res => {
        wx.navigateBack({ //返回
          delta: 1
        })
      })
    });
  },
  preview(event) {
    let currentUrl = event.currentTarget.dataset.src
    let lists = event.currentTarget.dataset.lists
    wx.previewImage({
      current: currentUrl, // 当前显示图片的http链接
      urls: lists // 需要预览的图片http链接列表
    })
  },

  //弹出二次删除选项
  openDialog() {
    this.setData({
      dialogShow: true
    })
  },

  tapDialogButton(e) {
    let index = e.detail.index;
    if (index == 0) {
      this.setData({
        dialogShow: false
      })
    } else {
      this.setData({
        dialogShow: false
      })
      this.delete()
    }
  },

  //获取VIP价格
  getVipPrice(e) {
    this.setData({
      vipPrice: e.detail.value
    })
    // if (this.data.vipPriceState == false) {
    //   this.setData({
    //     vipPrice: this.data.memberPrice
    //   })
    // } else {
    //   this.setData({
    //     vipPrice: e.detail.value
    //   })
    // }
  },

  //获取股东价格
  getShareholderPrice(e) {
    this.setData({
      shareholderPrice: e.detail.value
    })
    // if (this.data.shareholderPriceState == false) {
    //   this.setData({
    //     shareholderPrice: this.data.memberPrice
    //   })
    // } else {
    //   this.setData({
    //     shareholderPrice: e.detail.value
    //   })
    // }
  },


  //获取代理商价格
  getAgentPrice(e) {
    this.setData({
      agentPrice: e.detail.value
    })
  }

  //判断权重是否重复
  // judgeSort(){
  //   let goodsId=""
  //   if(this.data.goodsId){
  //     goodsId=this.data.goodsId
  //   }
  //   let sort=this.data.sort
  //   let shopId=wx.getStorageSync('shopId')
  //   Controllers.judgeSort({goodsId:goodsId,shopId:shopId,sort:sort}).then(res=>{

  //   })
  // }
})